package com.hdaccp.shuiguoD.controller;

import com.hdaccp.shuiguoD.dao.PermissionDao;
import com.hdaccp.shuiguoD.dao.RoleDao;
import com.hdaccp.shuiguoD.dao.RolePermissionDao;
import com.hdaccp.shuiguoD.entity.Permission;
import com.hdaccp.shuiguoD.entity.Role;
import com.hdaccp.shuiguoD.pogo.PermissionBean;
import com.hdaccp.shuiguoD.pogo.StateBean;
import com.hdaccp.shuiguoD.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/role")
@Scope("prototype")
public class RoleController {
    @Autowired
    private RoleDao roleDao;

    @Autowired
    private PermissionDao permissionDao;

    @Autowired
    private RoleService roleService;

    @Autowired
    private RolePermissionDao rolePermissionDao;


    @RequestMapping("/queryAll")
    @ResponseBody
    public Map<String, Object> queryAll(int offset, int limit) {
       /* Map<String,Object> map = new HashMap<>() ;
        map.put("rows",userInfoService.findAll(offset,limit)) ;
        map.put("total",userInfoService.findTotalCount()) ;
        return  map ;*/

        return null;
    }


    @RequestMapping("/doFindAll")
    @ResponseBody
    public Map<String, Object> doFindAll(@RequestParam(value = "rname", required = false, defaultValue = "") String rname, int limit, int offset) {
        Map<String, Object> map = new HashMap<>();
        map.put("total",roleService.findCount(rname)) ;
        map.put("rows",roleService.findAll(rname,limit,offset)) ;
        return map ;
    }

    @RequestMapping("/query")
    @ResponseBody
    public List<Role> query(int rid){
        //查询当前用户所有拥有的角色
        Role r = roleDao.getRoleByUserId(rid) ;
        //查询所有角色
        List<Role> list = roleDao.queryAll() ;
        for(Role role:list){
            if(r.getRid() == role.getRid()){
                role.setIschekced(true);
            }
        }
        return list ;
    }

    @RequestMapping("/doFenPen")
    @ResponseBody
    public List<PermissionBean> doFenPen(int rid){
        List<Permission> listAll = rolePermissionDao.queryByRoleId(rid);
        //获取根节点
        List<PermissionBean> list = permissionDao.query(0) ;
        for(PermissionBean permissionBean :list){
            long id = permissionBean.getPid() ;
            //查询子节点
            List<PermissionBean> list1 = permissionDao.query(id) ;
            permissionBean.setNodes(list1);
        }
        //回显当前角色拥有的所有权限
        for (PermissionBean per : list){
            StateBean stateBean = new StateBean();
            for (Permission permission : listAll){
                if (per.getPid() == permission.getPid()){
                    stateBean.setChecked(true);
                }

            }
            per.setState(stateBean);
            //所有的子节点
            List<PermissionBean> listSon = per.getNodes();
            for (PermissionBean permission1 : listSon){
                StateBean stateBean1 = new StateBean();
                for (Permission permission11 : listAll){
                    if (permission1.getPid() == permission11.getPid()){
                        stateBean.setChecked(true);
                        stateBean1.setChecked(true);
                    }
                }
                permission1.setState(stateBean1);
            }
        }




        return list ;
    }



}
